// es6 --  layer
// 简化小程序弹框 api
const comUtil = require('@/utils/common')
const globalConfig = require('@/config')

const layerConfig = {
  // 取消按钮颜色
  cancelColor: '#000000',
  // 确定按钮颜色
  confirmColor: globalConfig.themeColor || ''
}

/*
    轻提示
    tipProp String | Object
    return Promise
*/

function $tip(tipProp) {
  return new Promise(resolve => {
    if (!tipProp) {
      throw Error('tipProp is not defind')
    }
    const defaultTipProp = {
      icon: 'none',
      mask: false,
      duration: 1000 * 2.5
    }
    const tipPropType = comUtil.toType(tipProp)
    if (tipPropType === 'String') {
      defaultTipProp.title = tipProp
    } else if (tipPropType === 'Object') {
      comUtil.merge(defaultTipProp, tipProp)
    } else {
      throw Error('tipProp type must be String or Object')
    }

    wx.showToast({
      ...defaultTipProp,
      success() {
        // tip关闭后 callback
        if (defaultTipProp.hide && typeof defaultTipProp.hide === 'function') {
          setTimeout(() => { defaultTipProp.hide() }, defaultTipProp.duration)
        }
        resolve()
      },
      fail(error) {
        throw Error(error)
      }
    })
  })
}

/*
    错误捕获回调函数
    error String | ErrorObject | Object
    return Promise
*/
function $failTip(error, isTip = true) {
  return new Promise(resolve => {
    const errorType = comUtil.toType(error)
    let errorTipText
    if (errorType === 'Error') {
      errorTipText = error.message
    } else if (errorType === 'String') {
      errorTipText = error
    } else if (errorType === 'Object') {
      errorTipText = error.errMsg
    } else {
      throw Error('error type must be Error 、String or Object')
    }
    if (errorTipText) {
      console.log('Error Msg: ' + errorTipText)
      if (isTip) {
        return $tip(errorTipText)
      }
      resolve()
    } else {
      throw Error('errorTipText must be required')
    }
  })
}

/*
    对话框原型
    modalProp: String | Object
    otherModalProp: Object
    return -> Promise
*/
function showModal(modalProp, otherModalProp) {
  return new Promise((resolve, reject) => {
    if (!modalProp) {
      throw Error('modalProp must be required')
    }
    const defaultModalProp = {
      confirmColor: layerConfig.confirmColor,
      cancelColor: layerConfig.cancelColor
    }
    const modalPropType = comUtil.toType(modalProp)
    if (modalPropType === 'String') {
      defaultModalProp.content = modalProp
    } else if (modalPropType === 'Object') {
      Object.assign(defaultModalProp, modalProp)
    } else {
      throw Error('modalProp type must be String or Object')
    }
    if (otherModalProp) {
      Object.assign(defaultModalProp, otherModalProp)
    }
    if (!defaultModalProp.content) {
      throw Error('modalProp content is not defined')
    }
    wx.showModal({
      ...defaultModalProp,
      success(res) {
        if (res.confirm) {
          resolve(res)
        } else if (res.cancel) {
          reject(res)
        }
      },
      fail(error) {
        reject(error)
      }
    })
  })
}

/*
    对话框
        alertProp String | Object
        return Promise
*/
function $alert(alertProp) {
  return showModal(alertProp, {
    showCancel: false
  })
}

/*
    确认对话框
    confirmProp String | Object
    return Promise
*/

function $confirm(confirmProp) {
  return showModal(confirmProp)
}

module.exports = {
  $tip,
  $failTip,
  $alert,
  $confirm
}
